'''
        response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None,
        response_model_exclude: Optional[Union[SetIntStr, DictIntStrAny]] = None,
        response_model_by_alias: bool = True,
        response_model_exclude_unset: bool = False,
        response_model_exclude_defaults: bool = False,
        response_model_exclude_none: bool = False,
'''

# response_model_exclude_unset: bool = False,
# 有时候数据会有默认值，比如数据库中设置了默认值，不想返回这些默认值怎么办？
#  response_model_exclude_unset=True  设置该参数后就不会返回默认值，只会返回实际设置的值，假设没设置值，则不返回该字段

from typing import Optional, List

import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel, EmailStr

app = FastAPI()


class Item(BaseModel):
    name: str
    price: float
    # 下面三个字段有默认值
    description: Optional[str] = None
    tax: float = 10.5
    tags: List[str] = []


items = {
    "foo": {"name": "Foo", "price": 50.2},
    "bar": {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
    "baz": {"name": "Baz", "description": None, "price": 50.2, "tax": 10.5, "tags": []},
}


@app.get('/items/{item_id}', response_model=Item, response_model_exclude_unset=True)
async def read_item(item_id: str):
    # 从上面 items 字典中，根据 item_id 取出对应的值并返回
    return items[item_id]


'''
    response_model_include、response_model_exclude
    作用
    include：包含
    exclude：排除
    其实就是响应模型只要包含/排除有些属性
'''


# response_model_include 的栗子
# 结合上面注册功能的栗子：请求要密码，响应不要密码

class User(BaseModel):
    username: str
    password: str
    email: EmailStr
    full_name: Optional[str] = None


@app.post("/user/", response_model=User, response_model_include={'username', 'email', 'full_name'})
async def create_user(user: User):
    return user


if __name__ == '__main__':
    uvicorn.run(app='main3:app', host='127.0.0.1', port=8000, reload=True, debug=True)
